﻿using System.Collections.Generic;
using Downloader.Domain;
using Downloader.Framework.Queries;
using Downloader.Infrastructure;
using Downloader.Web.Controllers.ViewModels;
using SharpArch.NHibernate;

namespace Downloader.Web.Controllers.Queries
{
    public class FriendlyLinkQuery : NHibernateQuery,IFriendlyLinkQuery
    {
        public RecordList<FriendlyLinkViewModel> GetList(int page, int size)
        {
            var query = Session.QueryOver<FriendlyLink>().OrderBy(c => c.CreateTime).Desc;
            var count = query.ToRowCountQuery();
            var totalCount = count.FutureValue<int>();
            var firstResult = (page - 1)*size;
            var recordList = new RecordList<FriendlyLinkViewModel>();
            recordList.Data = new List<FriendlyLinkViewModel>();
            var model = query.Skip(firstResult).Take(size).Future();
            FriendlyLinkViewModel temp = null;
            foreach (var m in model)
            {
                temp=new FriendlyLinkViewModel(m);
                recordList.Data.Add(temp);
            }
            recordList.PageIndex = page;
            recordList.PageSize = size;
            recordList.RecordCount = totalCount.Value;
            return recordList;
        }
    }
}